<script>
const app = getApp();
import { 
	orderPay,   //订单支付--微信
	moneyPay    //订单支付--余额
} from "@/lib/api/orderServer/order";
export default {
	data() {
		return {
			/* 20200805 */
			orderId:"", //订单id
			orderInfo:{},  //订单信息
			showPopPay:false, //显示隐藏弹层
			payMoneyType:"",  //选择的支付方式
			/* 20200817 */
			/* 订单类型 */
			orderTypeActive:0,
			orderType:"15",
			orderTypes:[
				{
					type:"15",
					typeName:"到店自取",
					tagName:"到店"
				},
				{
					type:"10",
					typeName:"线上订单",
					tagName:"快递"
				}
			],
			/* 
			
				订单类型--状态
				10线上订单，15到店自取，20收银台收款
			*/
			orderTypeForState:{
				"10":{
					type:"10",
					typeName:"线上订单"
				},
				"15":{
					type:"15",
					typeName:"到店自取"
				},
				"20":{
					type:"20",
					typeName:"收银台收款"
				}
			},
			/* 
				订单状态:
				100待付款，150拼团已付款待成团，200待发货，250待到店，300已发货，400已完成，500已取消
			 */
			orderStatus:{
				"100":{
					status:"100",
					statusName:"待付款"
				},
				"150":{
					status:"150",
					statusName:"拼团已付款待成团"
				},
				"200":{
					status:"200",
					statusName:"待发货"
				},
				"250":{
					status:"250",
					statusName:"待到店"
				},
				"300":{
					status:"300",
					statusName:"已发货"
				},
				"400":{
					status:"400",
					statusName:"已完成"
				},
				"500":{
					status:"500",
					statusName:"已取消"
				}
			}
		};
	},
	methods: {
		// 选择订单类型
		async selectOrderType(index){
			//console.log(index);
			this.orderTypeActive = index;
			this.orderType = this.orderTypes[this.orderTypeActive].type;
			//
			//console.log(this.orderType);
			await this.onTallyOrder()
		},
		/* 
			支付失败之后
		*/
		payFailAfter(){
			uni.redirectTo({
				url: "/pages/user/pages/order/orderList"
			});
		},
		/* 
			付款成功之后---跳转至订单列表页
		*/
		paySuccessAfter(){
			uni.redirectTo({
				url: "/pages/user/pages/order/orderList"
			});
		},
		/* 
			支付
		*/
		callPay(val,callback){
			callback(true);
		},
		/* 
			支付之前
		*/
		callPayBefore(val,callback){
			callback(true);
		},
		/* 
			选择支付方式
		*/
		selectPayMoneyTypeAfter(payType){
			this.payMoneyType = payType;
		},
		/* 
			显示选择支付的弹层
		*/
		openPopPay(){
			this.showPopPay = true;
		},
		/* 
			关闭选择支付的弹层
		*/
		closePopPay(){
			this.showPopPay = false;
		},
		/* 
			orderid:订单id
			payType:选择的支付方式 (0:微信 1:余额)
		*/
		async onPay(orderid,payType=0) {
			switch(payType){
				case 0:
					const payData = await this.getPayData(orderid);
					return await this.payment(payData);
					break;
				case 1:
					return await moneyPay({orderId:orderid});
					break;
			}
			
		},
		//
		getPayData(orderId) {
			const propsData = {
				orderId
			};
			//#ifdef MP-WEIXIN
			return orderPay(propsData).then(res => {
				return res.data;
			});
			//#endif
		},
		//调起微信支付
		payment: function(payData) {
			return new Promise((resolve, reject) => {
				const getOrderStatus =
					(payData.service &&
						payData.service === 1 &&
						payData.getOrderStatus) ||
					null;
				uni.requestPayment({
					provider: payData.provider || "",
					orderInfo: payData.orderInfo || "",
					timeStamp: payData.timeStamp || "",
					nonceStr: payData.nonceStr || "",
					package: payData.packages || "",
					signType: payData.signType || "",
					paySign: payData.paySign || "",
					bannedChannels: payData.bannedChannels || [], // 需要隐藏的支付方式 =>百度小程序
					service: payData.service || 1, // 支付方式=> 头条小程序
					getOrderStatus, // 支付方式=> 商户前端实现的查询支付订单状态方法（该方法需要返回个Promise对象）service=3、4时不需要传。
					success: res => {
						resolve(res);
					},
					fail: error => {
						reject(error);
					}
				});
			});
		}
	}
};
</script>
